package giaodien;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPasswordField;

import controller.UserController;
import javax.swing.ImageIcon;

public class DangNhap extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	static String user;
	private String pass;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DangNhap frame = new DangNhap();
					frame.setVisible(true);
					frame.setSize(440, 300);
					frame.setLocation(100, 100);
					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DangNhap() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("");
		label.setBounds(252, 27, 46, 14);
		contentPane.add(label);

		JLabel lblPhnMmQun = new JLabel(
				"PHẦM MỀM QUẢN LÝ CỬA HÀNG ĐIỆN MÁY");
		lblPhnMmQun.setForeground(new Color(0, 0, 128));
		lblPhnMmQun.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblPhnMmQun.setBounds(21, 15, 395, 49);
		contentPane.add(lblPhnMmQun);

		JLabel lblTnngNhp = new JLabel("Tên đăng nhập");
		lblTnngNhp.setBounds(95, 88, 111, 19);
		contentPane.add(lblTnngNhp);

		textField = new JTextField();
		textField.setBounds(95, 115, 126, 20);
		contentPane.add(textField);
		textField.setColumns(10);

		JLabel lblMtKhu = new JLabel("Mật khẩu");
		lblMtKhu.setBounds(95, 145, 111, 19);
		contentPane.add(lblMtKhu);

		passwordField = new JPasswordField();
		passwordField.setBounds(95, 169, 126, 20);
		contentPane.add(passwordField);

		final JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setForeground(Color.RED);
		lblNewLabel.setBounds(252, 123, 126, 14);
		contentPane.add(lblNewLabel);

		final JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setForeground(Color.RED);
		lblNewLabel_1.setBounds(252, 148, 164, 14);
		contentPane.add(lblNewLabel_1);

		JButton btnngNhp = new JButton("Đăng nhập");
		btnngNhp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String user = textField.getText();
				String pass = passwordField.getText();
				if (UserController.checkUser(user, pass)) {
					TrangChu t = new TrangChu(user);
					t.setVisible(true);
					dispose();
				} else {
					lblNewLabel.setText("Tên đăng nhập hoặc ");
					lblNewLabel_1.setText("mật khẩu không đúng");
					passwordField.setText("");
				}
			}
		});
		btnngNhp.setBounds(95, 214, 97, 23);
		contentPane.add(btnngNhp);

		JButton btnHy = new JButton("Hủy");
		btnHy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnHy.setBounds(205, 214, 67, 23);
		contentPane.add(btnHy);

		JLabel label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon(DangNhap.class.getResource("/icon/key.png")));
		label_1.setBounds(10, 103, 75, 100);
		contentPane.add(label_1);

	}
}
